package com.myblog.controller;

import com.myblog.Utils.BeanCopyUtils;
import com.myblog.domain.GlobalResponse;
import com.myblog.domain.Vo.LinkVo;
import com.myblog.domain.entity.Link;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.services.LinkService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/content/link")
public class LinkController {
    @Resource
    LinkService linkService;


    @GetMapping("/list")
    public GlobalResponse getLinkList(Integer pageNum,Integer pageSize,String name,String status){
        return linkService.getLinkList(pageNum,pageSize,name,status);
    }
    @PostMapping()
        public GlobalResponse addLink(@RequestBody LinkVo linkVo){
        Link link = BeanCopyUtils.copyBean(linkVo, Link.class);
       if(linkService.save(link)){
           return GlobalResponse.ok();
       }
       return GlobalResponse.error();
    }

    @GetMapping("/{id}")
    public GlobalResponse getLink(@PathVariable Long id){
        Link link = linkService.getById(id);
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return GlobalResponse.ok(linkVo);
    }

    @PutMapping()
    public GlobalResponse updateLink(@RequestBody LinkVo linkVo){
        boolean b = linkService.updateById(BeanCopyUtils.copyBean(linkVo, Link.class));
        if(b){return GlobalResponse.ok();
        }
        return GlobalResponse.error(GlobalResponseEnum.ID_NO_EXIST);
    }

    @DeleteMapping("/{id}")
    public  GlobalResponse deleteLink(@PathVariable Long id){
        boolean b = linkService.removeById(id);
        if(b){
            return GlobalResponse.ok();
        }
        return GlobalResponse.error(GlobalResponseEnum.ID_NO_EXIST);
    }

}
